﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using YArchitech.Controls;
using YArchitech.LIB;
using libxl;

namespace YArchitech.Plumbing
{
	public class ExportExcel
	{
		public ExportExcel(HYSystemCal hySystemCal)
		{
			this.hySystemCal = hySystemCal;
		}

		public HYSystemCal.SystemType SysType { get; set; }

		public ExcelData ExcelDataToExport { get; set; }

		private string GetColumnName(int columnIndex)
		{
			columnIndex--;
			if (columnIndex < 26)
			{
				char c = (char)(columnIndex + 65);
				return string.Format("{0}", c);
			}
			if (columnIndex > 51)
			{
				throw new System.Exception("列数太多了，暂时不支持");
			}
			columnIndex -= 26;
			return string.Format("{0}{1}", "A", (char)(columnIndex + 65));
		}

		private void SetBkWorInfo(int step, string messge = "")
		{
			if (this.backWorker == null)
			{
				return;
			}
			this.rate += step;
			if (this.rate > 100)
			{
				this.rate = 100;
			}
			if (string.IsNullOrEmpty(messge))
			{
				this.backWorker.ReportProgress(this.rate);
				return;
			}
			this.backWorker.ReportProgress(this.rate, messge);
		}

		public bool ExportIntoExcel(HYSystemCal hySystemCal, BackgroundWorker bkWorker = null)
		{
			this.SysType = hySystemCal.systemType;
			bool result;
			switch (hySystemCal.systemType)
			{
			case HYSystemCal.SystemType.Drainage:
				result = this.ExportIntoExcel(bkWorker);
				break;
			case HYSystemCal.SystemType.Feed:
				result = this.ExportIntoExcelFeed(bkWorker);
				break;
			case HYSystemCal.SystemType.Fire:
				result = this.ExportIntoExcelFire(bkWorker);
				break;
			case HYSystemCal.SystemType.Spraying:
				result = this.ExportData(bkWorker);
				break;
			default:
				result = this.ExportData(bkWorker);
				break;
			}
			return result;
		}

		public bool ExportIntoExcel(BackgroundWorker bkWorker)
		{
			bool result = true;
			this.backWorker = bkWorker;
			try
			{
				this.SetBkWorInfo(0, "启动Excel...");
				this.book = new BinBook();
				this.book.setKey("Beijing YArchitech Tongxing Technology Co.,Ltd", "windows-282b2c0f08cbe70966b4616ea0i8d7n0");
				this.SetBkWorInfo(10, "启动Excel, 创建工作簿...");
				this.bkSheet = this.book.addSheet("排水系统水力计算表");
				int rowIndex = 0;
				this.SetBkWorInfo(2, "开始写计算数据...");
				this.SetBkWorInfo(2, "正在输出标题...");
				this.ExportHeader(ref rowIndex, this.bkSheet, 0, 8, "排水系统水力计算表");
				this.ExportDrainSetting(ref rowIndex, 8);
				this.ExportMainPipeDra(ref rowIndex, "计算结果：");
				this.SetBkWorInfo(11, "正在输出所有管段信息...");
				this.ExportPipeDra(ref rowIndex, this.hySystemCal.PipeCals);
				this.ExportCalcData(rowIndex);
				string text = "排水计算水力计算表";
				string saveExcelPath = this.GetSaveExcelPath(ref text);
				this.book.save(saveExcelPath);
				Process.Start(saveExcelPath);
			}
			catch (System.Exception)
			{
				throw;
			}
			return result;
		}

		public bool ExportIntoExcelFeed(BackgroundWorker bkWorker)
		{
			bool result = true;
			this.backWorker = bkWorker;
			try
			{
				this.SetBkWorInfo(0, "启动Excel...");
				this.book = new BinBook();
				this.book.setKey("Beijing YArchitech Tongxing Technology Co.,Ltd", "windows-282b2c0f08cbe70966b4616ea0i8d7n0");
				this.SetBkWorInfo(10, "启动Excel, 创建工作簿...");
				this.bkSheet = this.book.addSheet("给水系统水力计算表");
				int rowIndex = 0;
				this.SetBkWorInfo(2, "开始写计算数据...");
				this.SetBkWorInfo(2, "正在输出标题...");
				this.ExportHeader(ref rowIndex, this.bkSheet, 0, 8, "给水系统水力计算表");
				this.ExportFeedSetting(ref rowIndex, 8);
				this.ExportMainPipeFeed(ref rowIndex, "计算结果：");
				this.SetBkWorInfo(11, "正在输出所有管段信息...");
				this.ExportPipeFeed(ref rowIndex, this.hySystemCal.PipeCals);
				this.ExportCalcData(rowIndex);
				string text = "给水计算水力计算表";
				string saveExcelPath = this.GetSaveExcelPath(ref text);
				this.book.save(saveExcelPath);
				Process.Start(saveExcelPath);
			}
			catch (System.Exception ex)
			{
				string message = ex.Message;
				throw;
			}
			return result;
		}

		public bool ExportIntoExcelFire(BackgroundWorker bkWorker)
		{
			bool result = true;
			this.backWorker = bkWorker;
			try
			{
				this.SetBkWorInfo(0, "启动Excel...");
				this.book = new BinBook();
				this.book.setKey("Beijing YArchitech Tongxing Technology Co.,Ltd", "windows-282b2c0f08cbe70966b4616ea0i8d7n0");
				this.SetBkWorInfo(10, "启动Excel, 创建工作簿...");
				this.bkSheet = this.book.addSheet("消火栓系统水力计算表");
				int num = 0;
				this.SetBkWorInfo(2, "开始写计算数据...");
				this.SetBkWorInfo(2, "正在输出标题...");
				this.ExportHeader(ref num, this.bkSheet, 0, 13, "消火栓系统水力计算表");
				this.ExportFireSetting(ref num);
				this.ExportMainPipeFire(ref num, "所有管段的计算信息表");
				this.SetBkWorInfo(11, "正在输出所有管段信息...");
				this.ExportPipeFire(ref num, this.hySystemCal.PipeCals);
				num += 2;
				this.ExportCalcData(num);
				string text = "消火栓计算水力计算表";
				string saveExcelPath = this.GetSaveExcelPath(ref text);
				this.book.save(saveExcelPath);
				Process.Start(saveExcelPath);
			}
			catch (System.Exception)
			{
				throw;
			}
			return result;
		}

		private void ExportCalcData(int rowIndex)
		{
			this.ExportText(this.bkSheet, rowIndex, 0, 8, "", AlignH.ALIGNH_RIGHT);
			rowIndex++;
			this.ExportText(this.bkSheet, rowIndex, 0, 8, "设计软件：盈建科机电forRevit", AlignH.ALIGNH_RIGHT);
			rowIndex++;
			string str = DateTime.Now.ToString();
			this.ExportText(this.bkSheet, rowIndex, 0, 8, "计算时间:" + str, AlignH.ALIGNH_RIGHT);
		}

		private void ExportMainPipeDra(ref int rowIndex, string tableName)
		{
			this.ExportText(this.bkSheet, rowIndex, 0, 8, tableName);
			rowIndex++;
			int num = 0;
			Format format = this.ContentFormat(AlignH.ALIGNH_CENTER);
			format.setBorder(BorderStyle.BORDERSTYLE_THIN);
			this.bkSheet.writeStr(rowIndex, num, "编号", format);
			num++;
			this.bkSheet.writeStr(rowIndex, num, "管段编号", format);
			num++;
			this.bkSheet.writeStr(rowIndex, num, "坡度", format);
			num++;
			this.bkSheet.writeStr(rowIndex, num, "流量(L/s)", format);
			num++;
			this.bkSheet.writeStr(rowIndex, num, "流速(m/s)", format);
			num++;
			this.bkSheet.writeStr(rowIndex, num, "长度(m)", format);
			num++;
			this.bkSheet.writeStr(rowIndex, num, "公称直径(mm)", format);
			this.bkSheet.setCol(num, 12.0);
			num++;
			this.bkSheet.writeStr(rowIndex, num, "管道充满度", format);
			num++;
			this.bkSheet.writeStr(rowIndex, num, "当量", format);
			rowIndex++;
		}

		private void ExportPipeDra(ref int rowIndex, IList<HYPipeCal> listPipe)
		{
			for (int i = 0; i < listPipe.Count; i++)
			{
				HYPipeCal hypipeCal = listPipe[i];
				int num = 0;
				Format format = this.ContentFormat(AlignH.ALIGNH_CENTER);
				this.bkSheet.writeNum(rowIndex, num, (double)(i + 1), format);
				num++;
				this.bkSheet.writeStr(rowIndex, num, hypipeCal.Number, format);
				num++;
				this.bkSheet.writeNum(rowIndex, num, Math.Round(hypipeCal.dGrad, 2), format);
				num++;
				this.bkSheet.writeNum(rowIndex, num, Math.Round(hypipeCal.Flow, 2), format);
				num++;
				this.bkSheet.writeNum(rowIndex, num, Math.Round(hypipeCal.Velocity, 2), format);
				num++;
				this.bkSheet.writeNum(rowIndex, num, Math.Round(hypipeCal.Length, 3), format);
				num++;
				this.bkSheet.writeNum(rowIndex, num, (double)hypipeCal.nDN, format);
				num++;
				this.bkSheet.writeNum(rowIndex, num, Math.Round(hypipeCal.dFullness, 2), format);
				num++;
				this.bkSheet.writeNum(rowIndex, num, Math.Round(hypipeCal.Equivalent(this.SysType), 2), format);
				rowIndex++;
			}
		}

		private void ExportMainPipeFeed(ref int rowIndex, string tableName)
		{
			this.ExportText(this.bkSheet, rowIndex, 0, 8, tableName);
			rowIndex++;
			int num = 0;
			Format format = this.ContentFormat(AlignH.ALIGNH_CENTER);
			format.setBorder(BorderStyle.BORDERSTYLE_THIN);
			this.bkSheet.writeStr(rowIndex, num, "编号", format);
			num++;
			this.bkSheet.writeStr(rowIndex, num, "管段编号", format);
			num++;
			this.bkSheet.writeStr(rowIndex, num, "流量(L/s)", format);
			num++;
			this.bkSheet.writeStr(rowIndex, num, "流速(m/s)", format);
			num++;
			this.bkSheet.writeStr(rowIndex, num, "水力坡降(mH2O/m)", format);
			this.bkSheet.setCol(num, 17.0);
			num++;
			this.bkSheet.writeStr(rowIndex, num, "长度(m)", format);
			num++;
			this.bkSheet.writeStr(rowIndex, num, "公称直径(mm)", format);
			this.bkSheet.setCol(num, 12.0);
			num++;
			this.bkSheet.writeStr(rowIndex, num, "当量", format);
			num++;
			this.bkSheet.writeStr(rowIndex, num, "沿程阻力(m)", format);
			this.bkSheet.setCol(num, 13.0);
			rowIndex++;
		}

		private void ExportPipeFeed(ref int rowIndex, IList<HYPipeCal> listPipe)
		{
			for (int i = 0; i < listPipe.Count; i++)
			{
				HYPipeCal hypipeCal = listPipe[i];
				int num = 0;
				Format format = this.ContentFormat(AlignH.ALIGNH_CENTER);
				this.bkSheet.writeNum(rowIndex, num, (double)(i + 1), format);
				num++;
				this.bkSheet.writeStr(rowIndex, num, hypipeCal.Number, format);
				num++;
				this.bkSheet.writeNum(rowIndex, num, Math.Round(hypipeCal.Flow, 2), format);
				num++;
				this.bkSheet.writeNum(rowIndex, num, Math.Round(hypipeCal.Velocity, 2), format);
				num++;
				this.bkSheet.writeNum(rowIndex, num, Math.Round(hypipeCal.dLostWator, 3), format);
				num++;
				this.bkSheet.writeNum(rowIndex, num, Math.Round(hypipeCal.Length, 3), format);
				num++;
				this.bkSheet.writeNum(rowIndex, num, (double)hypipeCal.nDN, format);
				num++;
				this.bkSheet.writeNum(rowIndex, num, Math.Round(hypipeCal.Equivalent(this.SysType), 2), format);
				num++;
				this.bkSheet.writeNum(rowIndex, num, Math.Round(hypipeCal.LossPipe, 3), format);
				rowIndex++;
			}
		}

		private void ExportMainPipeFire(ref int rowIndex, string tableName)
		{
			this.ExportText(this.bkSheet, rowIndex, 0, 8, tableName);
			rowIndex++;
			int num = 0;
			Format format = this.ContentFormat(AlignH.ALIGNH_CENTER);
			format.setBorder(BorderStyle.BORDERSTYLE_THIN);
			this.bkSheet.writeStr(rowIndex, num, "编号", format);
			num++;
			this.bkSheet.writeStr(rowIndex, num, "管段编号", format);
			num++;
			this.bkSheet.writeStr(rowIndex, num, "流量(m3/s)", format);
			num++;
			this.bkSheet.writeStr(rowIndex, num, "流速(m/s)", format);
			num++;
			this.bkSheet.writeStr(rowIndex, num, "长度(m)", format);
			num++;
			this.bkSheet.writeStr(rowIndex, num, "公称直径(mm)", format);
			this.bkSheet.setCol(num, 12.0);
			num++;
			this.bkSheet.writeStr(rowIndex, num, "水力坡降(kPa/m)", format);
			this.bkSheet.setCol(num, 22.0);
			num++;
			this.bkSheet.writeStr(rowIndex, num, "沿程损失(kPa)", format);
			this.bkSheet.setCol(num, 18.0);
			num++;
			rowIndex++;
		}

		private void ExportPipeFire(ref int rowIndex, IList<HYPipeCal> listPipe)
		{
			Format format = this.ContentFormat(AlignH.ALIGNH_CENTER);
			double num = 0.0;
			for (int i = 0; i < listPipe.Count; i++)
			{
				HYPipeCal hypipeCal = listPipe[i];
				num += hypipeCal.dLostWator;
				int num2 = 0;
				this.bkSheet.writeNum(rowIndex, num2, (double)(i + 1), format);
				num2++;
				this.bkSheet.writeStr(rowIndex, num2, hypipeCal.Number, format);
				num2++;
				this.bkSheet.writeNum(rowIndex, num2, Math.Round(hypipeCal.Flow, 2), format);
				num2++;
				this.bkSheet.writeNum(rowIndex, num2, Math.Round(hypipeCal.Velocity, 2), format);
				num2++;
				this.bkSheet.writeNum(rowIndex, num2, Math.Round(hypipeCal.Length, 3), format);
				num2++;
				this.bkSheet.writeNum(rowIndex, num2, (double)hypipeCal.nDN, format);
				num2++;
				this.bkSheet.writeNum(rowIndex, num2, Math.Round(hypipeCal.dLostWator / hypipeCal.Length, 3), format);
				num2++;
				this.bkSheet.writeNum(rowIndex, num2, Math.Round(hypipeCal.dLostWator, 3), format);
				rowIndex++;
			}
			if (this.hySystemCal.ShowResults != null)
			{
				rowIndex++;
				this.bkSheet.setMerge(rowIndex, rowIndex, 0, 4);
				for (int j = 0; j <= 4; j++)
				{
					this.bkSheet.setCellFormat(rowIndex, j, format);
				}
				this.bkSheet.writeStr(rowIndex, 0, this.hySystemCal.ShowResults, format);
				rowIndex++;
			}
		}

		private void ExportHeader(ref int rowIndex, Sheet sheet, int colStart, int colEnd, string value)
		{
			sheet.setMerge(rowIndex, rowIndex, colStart, colEnd);
			sheet.writeStr(rowIndex, colStart, value, this.TitleFormat());
			rowIndex++;
		}

		private void ExportFeedSetting(ref int rowIndex, int colEnd)
		{
			Format format = this.ContentFormat(AlignH.ALIGNH_LEFT);
			format.setBorder(BorderStyle.BORDERSTYLE_NONE);
			this.bkSheet.setCol(0, 18.0);
			this.bkSheet.setCol(1, 24.0);
			if (this.hySystemCal.supplyParameter.formulaType == HYSupplyFormula.FormulaType.CALC_METHOD_AMOUNT)
			{
				this.bkSheet.setMerge(rowIndex, rowIndex, 0, colEnd);
				this.bkSheet.writeStr(rowIndex, 0, "根据《建筑给水排水设计规范》, 宿舍（I、II类）、旅馆、宾馆、酒店式公寓、医院、疗养院、幼儿园、", format);
				rowIndex++;
				this.bkSheet.setMerge(rowIndex, rowIndex, 0, colEnd);
				this.bkSheet.writeStr(rowIndex, 0, "养老院、办公楼、商场、图书馆、书店、客运站、航站楼、会展中心、中小学教学楼、公共厕所等建筑", format);
				rowIndex++;
				this.bkSheet.setMerge(rowIndex, rowIndex, 0, colEnd);
				this.bkSheet.writeStr(rowIndex, 0, "的生活给水设计秒流量，应按下式计算：", format);
				rowIndex++;
				this.bkSheet.setMerge(rowIndex, rowIndex, 0, colEnd);
				this.bkSheet.writeStr(rowIndex, 0, "qg = 0.2α√Ng", format);
				rowIndex++;
				this.bkSheet.setMerge(rowIndex, rowIndex, 0, colEnd);
				this.bkSheet.writeStr(rowIndex, 0, "式中：", format);
				rowIndex++;
				this.bkSheet.setMerge(rowIndex, rowIndex, 0, colEnd);
				this.bkSheet.writeStr(rowIndex, 0, "qg—计算管段的给水设计秒流量（L/s）;", format);
				rowIndex++;
				this.bkSheet.setMerge(rowIndex, rowIndex, 0, colEnd);
				this.bkSheet.writeStr(rowIndex, 0, "Ng—计算管段的卫生器具给水当量总数 ;", format);
				rowIndex++;
				this.bkSheet.setMerge(rowIndex, rowIndex, 0, colEnd);
				this.bkSheet.writeStr(rowIndex, 0, "α—根据建筑物用途而定的系数：1.5。", format);
				rowIndex++;
				return;
			}
			if (this.hySystemCal.supplyParameter.formulaType == HYSupplyFormula.FormulaType.CALC_METHOD_ZZ)
			{
				this.bkSheet.setMerge(rowIndex, rowIndex, 0, colEnd);
				this.bkSheet.writeStr(rowIndex, 0, "根据《建筑给水排水设计规范》，住宅建筑的给水管道的设计秒流量，应按下式计算：", this.TitleFormat());
				rowIndex++;
				this.bkSheet.setMerge(rowIndex, rowIndex, 0, colEnd);
				this.bkSheet.writeStr(rowIndex, 0, "1、计算最大用水时卫生器具给水当量平均出流概率：", format);
				rowIndex++;
				this.bkSheet.setMerge(rowIndex, rowIndex, 0, colEnd);
				this.bkSheet.writeStr(rowIndex, 0, "Uo = (100×qL×m×Kn)/(0.2×Ng×T×3600)", format);
				rowIndex++;
				this.bkSheet.setMerge(rowIndex, rowIndex, 0, colEnd);
				this.bkSheet.writeStr(rowIndex, 0, "式中：", format);
				rowIndex++;
				this.bkSheet.setMerge(rowIndex, rowIndex, 0, colEnd);
				this.bkSheet.writeStr(rowIndex, 0, "Uo—生活给水管道的最大用水时卫生器具给水当量平均出流概率（%）；", format);
				rowIndex++;
				string value = string.Format("qL—最高用水日的用水定额，取{0}（L/人•天)", this.hySystemCal.supplyParameter.dWaterNum);
				this.bkSheet.setMerge(rowIndex, rowIndex, 0, colEnd);
				this.bkSheet.writeStr(rowIndex, 0, value, format);
				rowIndex++;
				value = string.Format("m—每户用水人数，取{0}人；", this.hySystemCal.supplyParameter.nPersonNum);
				this.bkSheet.setMerge(rowIndex, rowIndex, 0, colEnd);
				this.bkSheet.writeStr(rowIndex, 0, value, format);
				rowIndex++;
				this.bkSheet.setMerge(rowIndex, rowIndex, 0, colEnd);
				this.bkSheet.writeStr(rowIndex, 0, "Ng—每户设置的卫生器具给水当量数；", format);
				rowIndex++;
				value = string.Format("T—用水时数，取{0}h；", this.hySystemCal.supplyParameter.dUserTime);
				this.bkSheet.setMerge(rowIndex, rowIndex, 0, colEnd);
				this.bkSheet.writeStr(rowIndex, 0, value, format);
				rowIndex++;
				this.bkSheet.setMerge(rowIndex, rowIndex, 0, colEnd);
				this.bkSheet.writeStr(rowIndex, 0, "", format);
				rowIndex++;
				this.bkSheet.setMerge(rowIndex, rowIndex, 0, colEnd);
				this.bkSheet.writeStr(rowIndex, 0, "2、计算卫生器具给水当量的同时出流概率：", format);
				rowIndex++;
				this.bkSheet.setMerge(rowIndex, rowIndex, 0, colEnd);
				this.bkSheet.writeStr(rowIndex, 0, "U=100(1+αc(Ng-1)^0.49)/√Ng", format);
				rowIndex++;
				this.bkSheet.setMerge(rowIndex, rowIndex, 0, colEnd);
				this.bkSheet.writeStr(rowIndex, 0, "U—计算管段的卫生器具给水当量同时出流概率（%）；", format);
				rowIndex++;
				this.bkSheet.setMerge(rowIndex, rowIndex, 0, colEnd);
				this.bkSheet.writeStr(rowIndex, 0, "αc—对应于不同Uo的系数；", format);
				rowIndex++;
				this.bkSheet.setMerge(rowIndex, rowIndex, 0, colEnd);
				this.bkSheet.writeStr(rowIndex, 0, "Ng—计算管段的卫生器具给水当量总数。", format);
				rowIndex++;
				this.bkSheet.setMerge(rowIndex, rowIndex, 0, colEnd);
				this.bkSheet.writeStr(rowIndex, 0, "", format);
				rowIndex++;
				this.bkSheet.setMerge(rowIndex, rowIndex, 0, colEnd);
				this.bkSheet.writeStr(rowIndex, 0, "3、计算管段的设计秒流量：", format);
				rowIndex++;
				this.bkSheet.setMerge(rowIndex, rowIndex, 0, colEnd);
				this.bkSheet.writeStr(rowIndex, 0, "         qg=0.2•U•Ng", format);
				rowIndex++;
				this.bkSheet.setMerge(rowIndex, rowIndex, 0, colEnd);
				this.bkSheet.writeStr(rowIndex, 0, "式中：", format);
				rowIndex++;
				this.bkSheet.setMerge(rowIndex, rowIndex, 0, colEnd);
				this.bkSheet.writeStr(rowIndex, 0, "qg—计算管段的设计秒流量（L/S）;", format);
				rowIndex++;
				this.bkSheet.setMerge(rowIndex, rowIndex, 0, colEnd);
				this.bkSheet.writeStr(rowIndex, 0, "U—计算管段的卫生器具给水当量同时出流概率（%）；", format);
				rowIndex++;
				this.bkSheet.setMerge(rowIndex, rowIndex, 0, colEnd);
				this.bkSheet.writeStr(rowIndex, 0, "Ng—计算管段的卫生器具给水当量总数。", format);
				rowIndex++;
				return;
			}
			if (this.hySystemCal.supplyParameter.formulaType == HYSupplyFormula.FormulaType.CALC_METHOD_PER)
			{
				this.bkSheet.setMerge(rowIndex, rowIndex, 0, colEnd);
				this.bkSheet.writeStr(rowIndex, 0, "根据《建筑给水排水设计规范》，宿舍（Ⅲ、Ⅳ类）工业企业生活间、公共浴室、职工食堂或营业", format);
				rowIndex++;
				this.bkSheet.setMerge(rowIndex, rowIndex, 0, colEnd);
				this.bkSheet.writeStr(rowIndex, 0, "餐馆的厨房、体育场馆、剧院、普通理化实验室等建筑的生活给水设计秒流量，应按下式计算：", format);
				rowIndex++;
				this.bkSheet.setMerge(rowIndex, rowIndex, 0, colEnd);
				this.bkSheet.writeStr(rowIndex, 0, "          qg=∑qonob", format);
				rowIndex++;
				this.bkSheet.setMerge(rowIndex, rowIndex, 0, colEnd);
				this.bkSheet.writeStr(rowIndex, 0, "qg—计算管段的给水设计秒流量（L/s）;", format);
				rowIndex++;
				this.bkSheet.setMerge(rowIndex, rowIndex, 0, colEnd);
				this.bkSheet.writeStr(rowIndex, 0, "qo—同类型的一个卫生器具给水额定流量（L/S）；", format);
				rowIndex++;
				this.bkSheet.setMerge(rowIndex, rowIndex, 0, colEnd);
				this.bkSheet.writeStr(rowIndex, 0, "no—同类型卫生器具数；", format);
				rowIndex++;
				this.bkSheet.setMerge(rowIndex, rowIndex, 0, colEnd);
				this.bkSheet.writeStr(rowIndex, 0, "b—同类型卫生器具的同时给水百分数。", format);
				rowIndex++;
			}
		}

		private void ExportDrainSetting(ref int rowIndex, int nCol)
		{
			Format format = this.ContentFormat(AlignH.ALIGNH_CENTER);
			format.setBorder(BorderStyle.BORDERSTYLE_NONE);
			this.bkSheet.setCol(0, 18.0);
			this.bkSheet.setCol(1, 25.0);
			if (this.hySystemCal.m_calDrain.nFormula == 0)
			{
				this.bkSheet.setMerge(rowIndex, rowIndex, 0, nCol);
				this.bkSheet.writeStr(rowIndex, 0, "根据《建筑给水排水设计规范》，住宅、宿舍（I、II类）、旅馆、酒店式公寓、医院、疗养院、幼", format);
				rowIndex++;
				this.bkSheet.setMerge(rowIndex, rowIndex, 0, nCol);
				this.bkSheet.writeStr(rowIndex, 0, "儿园、养老院、办公楼、商场、图书馆、书店、客运中心、航站楼、会展中心、中小学教学楼、食", format);
				rowIndex++;
				this.bkSheet.setMerge(rowIndex, rowIndex, 0, nCol);
				this.bkSheet.writeStr(rowIndex, 0, "堂或营业餐厅等建筑生活排水管道设计秒流量，应按下式计算：", format);
				rowIndex++;
				this.bkSheet.setMerge(rowIndex, rowIndex, 0, nCol);
				this.bkSheet.writeStr(rowIndex, 0, "              qp=0.12α√Np+qmax", format);
				rowIndex++;
				this.bkSheet.setMerge(rowIndex, rowIndex, 0, nCol);
				this.bkSheet.writeStr(rowIndex, 0, "式中：", format);
				rowIndex++;
				this.bkSheet.setMerge(rowIndex, rowIndex, 0, nCol);
				this.bkSheet.writeStr(rowIndex, 0, "qp—计算管段排水设计秒流量（L/s）;", format);
				rowIndex++;
				this.bkSheet.setMerge(rowIndex, rowIndex, 0, nCol);
				this.bkSheet.writeStr(rowIndex, 0, "Np—计算管段的卫生器具排水当量总数；", format);
				rowIndex++;
				this.bkSheet.setMerge(rowIndex, rowIndex, 0, nCol);
				this.bkSheet.writeStr(rowIndex, 0, "α—根据建筑物用途而定的系数；", format);
				rowIndex++;
				this.bkSheet.setMerge(rowIndex, rowIndex, 0, nCol);
				this.bkSheet.writeStr(rowIndex, 0, "qmax—计算管段上最大一个卫生器具的排水流量（L/s）。", format);
				rowIndex++;
				return;
			}
			this.bkSheet.setMerge(rowIndex, rowIndex, 0, nCol);
			this.bkSheet.writeStr(rowIndex, 0, "根据《建筑给水排水设计规范》，宿舍（Ⅲ、Ⅳ类）、工业企业生活间、公共浴室、洗衣房、职工", format);
			rowIndex++;
			this.bkSheet.setMerge(rowIndex, rowIndex, 0, nCol);
			this.bkSheet.writeStr(rowIndex, 0, "食堂或营业餐厅的厨房、实验室、影剧院、体育场（馆）等建筑的生活管道排水设计秒流量，应", format);
			rowIndex++;
			this.bkSheet.setMerge(rowIndex, rowIndex, 0, nCol);
			this.bkSheet.writeStr(rowIndex, 0, "按下式计算：", format);
			rowIndex++;
			this.bkSheet.setMerge(rowIndex, rowIndex, 0, nCol);
			this.bkSheet.writeStr(rowIndex, 0, "              qp=∑q0n0b", format);
			rowIndex++;
			this.bkSheet.setMerge(rowIndex, rowIndex, 0, nCol);
			this.bkSheet.writeStr(rowIndex, 0, "式中：", format);
			rowIndex++;
			this.bkSheet.setMerge(rowIndex, rowIndex, 0, nCol);
			this.bkSheet.writeStr(rowIndex, 0, "qp—计算管段排水设计秒流量（L/s）；", format);
			rowIndex++;
			this.bkSheet.setMerge(rowIndex, rowIndex, 0, nCol);
			this.bkSheet.writeStr(rowIndex, 0, "q0—同类型的一个卫生器具排水流量（L/s）;", format);
			rowIndex++;
			this.bkSheet.setMerge(rowIndex, rowIndex, 0, nCol);
			this.bkSheet.writeStr(rowIndex, 0, "n0—同类型卫生器具数；", format);
			rowIndex++;
			this.bkSheet.setMerge(rowIndex, rowIndex, 0, nCol);
			this.bkSheet.writeStr(rowIndex, 0, "b—卫生器具的同时排水百分数。", format);
			rowIndex++;
		}

		private void ExportFireSetting(ref int rowIndex)
		{
			Format format = this.ContentFormat(AlignH.ALIGNH_CENTER);
			this.bkSheet.setCol(0, 20.0);
			this.bkSheet.writeStr(rowIndex, 0, "充实水柱长度(m)：", format);
			this.bkSheet.writeNum(rowIndex, 1, this.hySystemCal.hyFireData.DMinLength, format);
			rowIndex++;
			this.bkSheet.writeStr(rowIndex, 0, "消火栓出水流量(L/s)：", format);
			this.bkSheet.writeNum(rowIndex, 1, this.hySystemCal.hyFireData.DFlow, format);
			rowIndex++;
			this.bkSheet.writeStr(rowIndex, 0, "消火栓口压力(mH2O)：", format);
			this.bkSheet.writeNum(rowIndex, 1, this.hySystemCal.hyFireData.DPressure, format);
			rowIndex++;
			this.bkSheet.writeStr(rowIndex, 0, "消火栓保护半径(m)：", format);
			this.bkSheet.writeNum(rowIndex, 1, this.hySystemCal.hyFireData.DRadius, format);
			rowIndex++;
		}

		private string GetBuildType()
		{
			string result = "";
			switch (this.hySystemCal.supplyParameter.formulaType)
			{
			case HYSupplyFormula.FormulaType.CALC_METHOD_AMOUNT:
				result = "公共建筑给水计算(当量法)";
				break;
			case HYSupplyFormula.FormulaType.CALC_METHOD_PER:
				result = "工业建筑给水计算(百分数法)";
				break;
			case HYSupplyFormula.FormulaType.CALC_METHOD_ZZ:
				result = "住宅给水计算(同时概念法)";
				break;
			}
			return result;
		}

		private string GetBuildUse()
		{
			string result = "";
			HYSupplyFormula.FormulaType formulaType = this.hySystemCal.supplyParameter.formulaType;
			if (formulaType != HYSupplyFormula.FormulaType.CALC_METHOD_AMOUNT)
			{
				if (formulaType == HYSupplyFormula.FormulaType.CALC_METHOD_ZZ)
				{
					HYCalSQLData.GetSupplyFamilyCoefficient();
					result = this.hySystemCal.supplyParameter.szBuildType;
				}
			}
			else
			{
				HYCalSQLData.GetSupplyBuildingCoefficientName();
				result = this.hySystemCal.supplyParameter.szBuildType;
			}
			return result;
		}

		private string GetFormulaType()
		{
			string result = "";
			switch (this.hySystemCal.m_calParameter.formulaType)
			{
			case HYCalParameter.FormulaType.Haihad_William:
				result = "海澄威廉公式";
				break;
			case HYCalParameter.FormulaType.Manning:
				result = "曼宁公式";
				break;
			case HYCalParameter.FormulaType.ColeBlok:
				result = "柯尔勃洛克公式";
				break;
			}
			return result;
		}

		private void GetFormulaPara(int rowIndex)
		{
			Format format = this.ContentFormat(AlignH.ALIGNH_CENTER);
			string value = "";
			double value2 = 0.0;
			switch (this.hySystemCal.m_calParameter.formulaType)
			{
			case HYCalParameter.FormulaType.Haihad_William:
				value = "系数C(海澄-威廉)";
				value2 = this.hySystemCal.m_calParameter.c;
				break;
			case HYCalParameter.FormulaType.Manning:
				value = "系数k(柯尔勃洛克):";
				value2 = this.hySystemCal.m_calParameter.k;
				break;
			case HYCalParameter.FormulaType.ColeBlok:
				value = "系数n(曼宁):";
				value2 = this.hySystemCal.m_calParameter.n;
				break;
			}
			this.bkSheet.writeStr(rowIndex, 0, value, format);
			this.bkSheet.writeNum(rowIndex, 1, value2, format);
		}

		private void ExportText(Sheet sheet, int rowIndex, int colStart, int colEnd, string value, AlignH alignH)
		{
			sheet.setMerge(rowIndex, rowIndex, colStart, colEnd);
			Format format = this.ContentFormat(alignH);
			format.setBorder(BorderStyle.BORDERSTYLE_NONE);
			sheet.writeStr(rowIndex, colStart, value, format);
		}

		private void ExportText(Sheet sheet, int rowIndex, int colStart, int colEnd, string value)
		{
			sheet.setMerge(rowIndex, rowIndex, colStart, colEnd);
			Format format = this.book.addFormat();
			format.font = this.TextFont();
			format.alignH = AlignH.ALIGNH_LEFT;
			format.alignV = AlignV.ALIGNV_CENTER;
			sheet.writeStr(rowIndex, colStart, value, format);
		}

		private void ExportText(Sheet sheet, string value, int rowIndex, int endRowIndex, int colNum)
		{
			sheet.setMerge(rowIndex, endRowIndex, colNum, colNum);
			Format format = this.book.addFormat();
			format.setBorder(BorderStyle.BORDERSTYLE_THIN);
			format.font = this.TextFont();
			format.alignH = AlignH.ALIGNH_CENTER;
			format.alignV = AlignV.ALIGNV_CENTER;
			for (int i = rowIndex; i <= endRowIndex; i++)
			{
				sheet.setCellFormat(i, colNum, format);
			}
			sheet.writeStr(rowIndex, colNum, value, format);
		}

		private void SetCellsFormat(Sheet sheet, int rowStart, int rowEnd, int colStart, int colEnd)
		{
			Format format = this.book.addFormat();
			format.setBorder(BorderStyle.BORDERSTYLE_THIN);
			format.font = this.TextFont();
			format.alignH = AlignH.ALIGNH_CENTER;
			format.alignV = AlignV.ALIGNV_CENTER;
			for (int i = rowStart; i <= rowEnd; i++)
			{
				for (int j = colStart; j <= colEnd; j++)
				{
					sheet.setCellFormat(i, j, format);
				}
			}
		}

		private Format TitleFormat()
		{
			Font font = this.book.addFont();
			font.size = 12;
			font.name = "黑体";
			Format format = this.book.addFormat();
			format.alignH = AlignH.ALIGNH_CENTER;
			format.alignV = AlignV.ALIGNV_CENTER;
			format.font = font;
			return format;
		}

		private Font TextFont()
		{
			Font font = this.book.addFont();
			font.size = 10;
			return font;
		}

		private Format TableFormat()
		{
			Font font = this.TextFont();
			Format format = this.book.addFormat();
			format.alignH = AlignH.ALIGNH_CENTER;
			format.alignV = AlignV.ALIGNV_CENTER;
			format.font = font;
			return format;
		}

		private Format ContentFormat(AlignH alignH)
		{
			Font font = this.TextFont();
			Format format = this.book.addFormat();
			format.font = font;
			format.setBorder(BorderStyle.BORDERSTYLE_THIN);
			format.alignV = AlignV.ALIGNV_CENTER;
			format.alignH = alignH;
			return format;
		}

		protected bool ExportData(BackgroundWorker bkWorker)
		{
			bool result = false;
			this.backWorker = bkWorker;
			try
			{
				if (this.ExcelDataToExport != null)
				{
					this.SetBkWorInfo(0, "启动Excel...");
					this.book = new BinBook();
					this.book.setKey("Beijing YArchitech Tongxing Technology Co.,Ltd", "windows-282b2c0f08cbe70966b4616ea0i8d7n0");
					this.SetBkWorInfo(10, "启动Excel, 创建工作簿...");
					this.bkSheet = this.book.addSheet(this.ExcelDataToExport.SheetName);
					int num = 0;
					this.SetBkWorInfo(2, "开始写计算数据...");
					this.SetBkWorInfo(2, "正在输出标题...");
					this.ExportHeader(ref num, this.bkSheet, 0, 13, this.ExcelDataToExport.SheetName);
					this.WriteForwardData(ref num);
					this.ExportText(this.bkSheet, num, 0, this.ExcelDataToExport.ColumnsData.Count - 1, this.ExcelDataToExport.Title);
					num++;
					this.WriteColumns(ref num);
					this.SetBkWorInfo(11, "正在输出所有信息...");
					this.WriteRows(ref num);
					this.WriteBackWardData(ref num);
					num += 2;
					this.ExportCalcData(num);
					if (string.IsNullOrWhiteSpace(this.ExcelDataToExport.FileName))
					{
						YJKMessageBox.Error("请指定一个有效的文件名！");
					}
					else
					{
						string fileName = this.ExcelDataToExport.FileName;
						string saveExcelPath = this.GetSaveExcelPath(ref fileName);
						this.book.save(saveExcelPath);
						Process.Start(saveExcelPath);
						result = true;
					}
				}
				else
				{
					YJKMessageBox.Error("请提供要写入的数据！");
				}
			}
			catch (System.Exception ex)
			{
				YJKMessageBox.Error(ex.ToString());
				throw;
			}
			return result;
		}

		private string GetSaveExcelPath(ref string intialPath)
		{
			string text = Path.Combine(Product.UserDataLocation, intialPath + ".xls");
			if (File.Exists(text))
			{
				intialPath++;
				text = this.GetSaveExcelPath(ref intialPath);
			}
			return text;
		}

		protected void WriteCell(ExcelData.Cell cell, int columnIndex, ref int rowIndex)
		{
			Format format = this.ContentFormat(AlignH.ALIGNH_CENTER);
			switch (cell.TypeOfCell)
			{
			case ExcelData.ExcelCellType.StringCell:
				this.bkSheet.writeStr(rowIndex, columnIndex, cell.Data, format);
				return;
			case ExcelData.ExcelCellType.Int16Cell:
				this.bkSheet.writeNum(rowIndex, columnIndex, (double)Convert.ToInt16(cell.Data), format);
				return;
			case ExcelData.ExcelCellType.Int32Cell:
				this.bkSheet.writeNum(rowIndex, columnIndex, (double)Convert.ToInt32(cell.Data), format);
				return;
			case ExcelData.ExcelCellType.Int64Cell:
				this.bkSheet.writeNum(rowIndex, columnIndex, (double)Convert.ToInt64(cell.Data), format);
				return;
			case ExcelData.ExcelCellType.doubleCell:
				this.bkSheet.writeNum(rowIndex, columnIndex, Math.Round(Convert.ToDouble(cell.Data), cell.Round), format);
				return;
			default:
				this.bkSheet.writeStr(rowIndex, columnIndex, cell.Data, format);
				return;
			}
		}

		protected void WriteOneRow(List<ExcelData.Cell> row, ref int rowIndex)
		{
			int num = 0;
			foreach (ExcelData.Cell cell in row)
			{
				this.WriteCell(cell, num, ref rowIndex);
				num++;
			}
			rowIndex++;
		}

		protected void WriteColumns(ref int rowIndex)
		{
			this.WriteOneRow(this.ExcelDataToExport.ColumnsData, ref rowIndex);
		}

		protected void WriteRows(ref int rowIndex)
		{
			foreach (List<ExcelData.Cell> row in this.ExcelDataToExport.RowsData)
			{
				this.WriteOneRow(row, ref rowIndex);
			}
		}

		protected void WriteForwardData(ref int rowIndex)
		{
			if (this.ExcelDataToExport.ForwardData != null)
			{
				rowIndex++;
				foreach (List<ExcelData.Cell> row in this.ExcelDataToExport.ForwardData)
				{
					this.WriteOneRow(row, ref rowIndex);
				}
				rowIndex++;
			}
		}

		protected void WriteBackWardData(ref int rowIndex)
		{
			if (!string.IsNullOrEmpty(this.ExcelDataToExport.BackwardData))
			{
				rowIndex++;
				this.ExportText(this.bkSheet, rowIndex, 0, this.ExcelDataToExport.ColumnsData.Count - 1, this.ExcelDataToExport.BackwardData, AlignH.ALIGNH_LEFT);
			}
		}

		private HYSystemCal hySystemCal;

		private int rate;

		private Book book;

		private Sheet bkSheet;

		private BackgroundWorker backWorker;

		private enum Align
		{
			Center,
			Right,
			Left
		}
	}
}
